package com.urbo.android.shortcutter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Main View of the application
 * 
 * @author radovan.urban@gmail.com
 * 
 * @version 1.0
 * 
 */
public class Main extends Activity implements OnClickListener {

	public static String				ITEM_TITLE					= "title";
	public static String				ITEM_TYPE					= "type";

	public static String				CUSTOM_CALL					= "1";
	public static String				CUSTOM_MESSAGE				= "2";
	public static String				CUSTOM_EMAIL				= "3";

	private static final int			CUSTOM_CALL_REQUEST_CODE	= 1;
	private static final int			CUSTOM_SMS_REQUEST_CODE		= 2;
	private static final int			CUSTOM_EMAIL_REQUEST_CODE	= 3;

	public Context						mContext;

	private Button						btnNew;
	private Button						btnCustom;
	private Button						btnExit;
	private ListSelectionDialog			customListDialog;
	private List<Map<String, String>>	listOfCustomChoices;
	private ShortcutDialog				shortcutDialog;

	private Bundle						dataForCustomShortcut;
	private String						phoneNumber;

	/** Start all possible View listeners */
	public void startListeners()
	{

		this.btnNew.setOnClickListener( this );
		this.btnCustom.setOnClickListener( this );
		this.btnExit.setOnClickListener( this );
	}

	/** Stop all possible View listeners */
	private void stopListeners()
	{

		this.btnNew.setOnClickListener( null );
		this.btnCustom.setOnClickListener( null );
		this.btnExit.setOnClickListener( null );
	}

	@Override
	public void onResume()
	{
		super.onResume();
		this.startListeners();
	}

	@Override
	public void onPause()
	{
		super.onPause();
		this.stopListeners();
	}

	// Activity body click listener
	public void onClick( View v )
	{

		switch ( v.getId() ) {
			case R.id.btn_exit_ref :
				Main.this.finish();
				break;

			case R.id.btn_new_ref :
				Intent iListActivities = new Intent( this, ActivityList.class );
				this.startActivity( iListActivities );
				break;

			case R.id.btn_custom_ref :
				customListDialog.show();
				break;

			default :
		}
	}

	// Click Handler of itemClick in ListSelectionDialog. Custom choice is
	// selected
	public ListSelectionDialog.OnClickListener	onCustomChoiceClickListener	= new ListSelectionDialog.OnClickListener() {

		@Override
		public void onClick( Map<String, String> selectedItem )
		{
			customListDialog.dismiss();
			Bundle activityParameters = new Bundle();

			Intent dialogIntent = new Intent( mContext, SmsDialog.class );

			String s = selectedItem.get( ITEM_TYPE );

			if ( s == CUSTOM_MESSAGE ) {
				activityParameters.putInt( SmsDialog.DIALOG_TYPE_KEY,
						SmsDialog.DIALOG_SMS );
				dialogIntent.putExtras( activityParameters );
				startActivityForResult( dialogIntent, CUSTOM_SMS_REQUEST_CODE );
			}

			if ( s == CUSTOM_CALL ) {
				Intent contactPickerIntent = new Intent( Intent.ACTION_PICK,
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI );
				startActivityForResult( contactPickerIntent, CUSTOM_CALL_REQUEST_CODE );
			}

			if ( s == CUSTOM_EMAIL ) {
				activityParameters.putInt( SmsDialog.DIALOG_TYPE_KEY,
						SmsDialog.DIALOG_EMAIL );
				dialogIntent.putExtras( activityParameters );
				startActivityForResult( dialogIntent, CUSTOM_EMAIL_REQUEST_CODE );
			}
		}
	};

	// Result handler for custom shortcuts activities (SMS, CALL, MAIL)
	protected void onActivityResult( int requestCode, int resultCode, Intent data )
	{
		// this.onActivityResult(requestCode, resultCode, data);
		if ( resultCode == RESULT_OK ) {
			switch ( requestCode ) {
				case CUSTOM_SMS_REQUEST_CODE :
					dataForCustomShortcut = data.getBundleExtra( SmsDialog.RESULT_BUNDLE );
					shortcutDialog.setShortcutTitle( "SMS to " );
					shortcutDialog.show( CUSTOM_SMS_REQUEST_CODE );
					break;

				case CUSTOM_CALL_REQUEST_CODE :
					phoneNumber = ContentUtils.getPhoneNumberFromPhoneUri( this, data.getData() );
					shortcutDialog.setShortcutTitle( "Call " );
					shortcutDialog.show( CUSTOM_CALL_REQUEST_CODE );
					break;

				case CUSTOM_EMAIL_REQUEST_CODE :
					dataForCustomShortcut = data.getBundleExtra( SmsDialog.RESULT_BUNDLE );
					shortcutDialog.setShortcutTitle( "Email " );
					shortcutDialog.show( CUSTOM_EMAIL_REQUEST_CODE );
					break;
			}

		} else {

		}
	}

	// Click handler for shortcutDialog
	public DialogInterface.OnClickListener	shortcutDialogClick	= new DialogInterface.OnClickListener() {

		@Override
		public void onClick( DialogInterface dialog, int which )
		{
			int returnCode = shortcutDialog.dismiss();

			if ( which == -2 ) return;

			String iconTitle = shortcutDialog.getShortcutTitle();
			BitmapDrawable iconDrawable = (BitmapDrawable) shortcutDialog.getShortcutIcon();

			switch ( returnCode ) {

				case CUSTOM_CALL_REQUEST_CODE :
					Shortcut.createCallShortcut( Main.this, iconTitle, iconDrawable, phoneNumber );
					break;

				case CUSTOM_SMS_REQUEST_CODE :
					Shortcut.createSmsShortcut( Main.this, iconTitle, iconDrawable,
							dataForCustomShortcut.getString( SmsDialog.PHONE_NUMBER ),
							dataForCustomShortcut.getString( SmsDialog.MESSAGE_TEXT ) );
					break;

				case CUSTOM_EMAIL_REQUEST_CODE :
					Shortcut.createEmailShortcut( Main.this, iconTitle, iconDrawable,
							dataForCustomShortcut.getString( SmsDialog.PHONE_NUMBER ),
							dataForCustomShortcut.getString( SmsDialog.SUBJECT ),
							dataForCustomShortcut.getString( SmsDialog.MESSAGE_TEXT ) );
					break;
				default :

			}
		}
	};

	private Map<String, String> createItem( String type, String title )
	{
		Map<String, String> newItem = new HashMap<String, String>();
		newItem.put( ITEM_TYPE, type );
		newItem.put( ITEM_TITLE, title );
		return newItem;
	}

	private void createCustomChoicesDialog()
	{
		listOfCustomChoices = new LinkedList<Map<String, String>>();
		listOfCustomChoices.add( createItem( CUSTOM_CALL, "Direct Call" ) );
		listOfCustomChoices.add( createItem( CUSTOM_MESSAGE, "Direct Message" ) );
		listOfCustomChoices.add( createItem( CUSTOM_EMAIL, "Direct Email" ) );

		customListDialog = new ListSelectionDialog( this );
		customListDialog.setDataSource( listOfCustomChoices );
		customListDialog.SetOnClickListener( onCustomChoiceClickListener );
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView( R.layout.main );

		this.mContext = this;
		// References to buttons on the screen
		btnNew = (Button) findViewById( R.id.btn_new_ref );
		btnCustom = (Button) findViewById( R.id.btn_custom_ref );
		btnExit = (Button) findViewById( R.id.btn_exit_ref );

		createCustomChoicesDialog();
		shortcutDialog = new ShortcutDialog( this, R.layout.activity_dialog, shortcutDialogClick );
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu )
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate( R.menu.menu, menu );
		return true;
	}

	@Override
	public boolean onOptionsItemSelected( MenuItem item )
	{
		Intent intent = new Intent(this, HelpViewer.class);
		try {
			switch ( item.getItemId() ) {
				case R.id.menu_help :
					intent.putExtra( "url", "file:///android_asset/help.html" );
					break;
				case R.id.menu_about :
					intent.putExtra( "url", "file:///android_asset/about.html" );
					break;
			}
			startActivity(intent);
			return true;
		}
		catch ( Exception e ) {
			return false;
		}
	}
}
